<?php
namespace App\Controllers;

/**
 * Class BaseController
 *
 * BaseController provides a convenient place for loading components
 * and performing functions that are needed by all your controllers.
 * Extend this class in any new controllers:
 *     class Home extends BaseController
 *
 * For security be sure to declare any new methods as protected or private.
 *
 * @package CodeIgniter
 */

use App\Controllers\BaseController;
use App\Models\ApisModel;

class ApiBase extends BaseController
{

    /**
     * An array of helpers to be loaded automatically upon
     * class instantiation. These helpers will be available
     * to all other controllers that extend BaseController.
     *
     * @var array
     */
    protected $helpers = [];
    protected $apisModel;
    protected $mokappkey;
    protected $mokappsecret;
    protected $moktoken;
    protected $mokapp; //当前接口应用信息
    protected $usertoken;
    protected $user;

    /**
     * Constructor.
     */
    public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);

        $this->apisModel = new ApisModel();

        $this->usertoken = $this->request->getHeaderLine('token');
        $this->moktoken = $this->request->getHeaderLine('moktoken');
        $this->mokappkey = $this->request->getHeaderLine('mokappkey');
        $this->mokappsecret = $this->request->getHeaderLine('mokappsecret');

        $this->checkApiToken();

        $this->getUserInfor();

    }

    protected function checkApiAppKey()
    {
        $data = $this->apisModel->getOpenByKey($this->mokappkey);
        if (!$data) {
            $this->_json(1, '应用不存在');
        }
        $this->mokapp = $data;
        $secret = sha1($data['secret'] . $data['platform'] . $data['key']);
        if ($secret != $this->mokappsecret) {
            $this->_json(1, '应用授权错误');
        }
        $this->moktoken = md5($secret . $data['salt']);
        //生成apptoken
        $this->cache->save($this->moktoken, ['id' => $data['id'], 'key' => $data['key'], 'userid' => $data['userid'], 'platform' => $data['platform']], MONTH);
        $this->_json(0, 'ok', $this->moktoken);
    }

    protected function checkApiToken()
    {
        ams_clean_xss($this->moktoken);
        $value = $this->cache->get($this->moktoken);
        if (!$value) {
            $this->checkApiAppKey();
        } else {
            $this->mokapp = $value;
        }
    }

    public function getUserInfor()
    {
        $cache = $this->cache->get($this->usertoken);
        if (!$cache) {
            $this->userid = 0;
            $this->user = null;
            return '';
        }
        if (isset($cache['userid']) && $cache['userid']) {
            $this->userid = $cache['userid'];
            $this->session->set('userid', $this->userid);
            $this->user = $this->userModel->getUser($this->userid);
            $this->renderer->setData(['user' => $this->user, 'userid' => $this->userid]);
        }

    }

}
